function writeSegFile(s, onsets, hop, filename, fs, nbits)
%function writeSegFile(x);

if (onsets(end)*hop ~= length(s))
    vet = [(onsets.*hop) length(s)]; % a ultima amostra do sinal deve ser tratada como "um ponto de onset"    
end

for i=1:length(vet)
	if (vet(i) <= 0) vet(i) = 1; end
end

for i=1:(length(vet)-1)
	nomeAux = cat(2, 'segmentacoes\', filename(1:length(filename)-4), '_', num2str(i), '.wav'); % nome para salvar o arquivo
	wavwrite(s(vet(i):vet(i+1)), fs, nbits, nomeAux);
    wavplay([s(vet(i):vet(i+1)); zeros(44100, 1)], fs);    
%    wavplay([s(vet(i):vet(i+1)); zeros(floor(0.5*(vet(i+1)-vet(i))), 1)], fs);
end
